// pages/mine/edit_detail.js

const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44)+'px';
const navTop = (App.statusBarHeight + 44 +5)+'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: '名前',
    navigationBarHeight,
    userInfo:{},
    isPhone:false,
    
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var isPhone = options.isPhone;

    // this.setData({
    //   userInfo:App.userInfo
    // })

    this.requestUserInfo();
    
    if(isPhone){
      this.setData({
        isPhone:true,
        navigationBarTitle:'携帯電話番号'
      })
    }
    new App.ToastPannel();
  },

  requestUserInfo: function () {
    var url = config.service.userInfoUrl;
    var params = {

    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {

      if (res.code == 0) {
        this.setData({
          userInfo:App.userInfo
        })
        App.userInfo = res.data;
      } else {
        this.show(res.mesage);
      }
    })
  },


  


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  submitClick:function(e){
    if(this.data.isPhone){
      
      if (this.data.userInfo.phone ==  null) {
        this.show('手机号不能为空');
        return;
      }
      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1}))+\d{8})$/;

      if (!myreg.test(this.data.userInfo.phone)) {
        this.show('请输入正确的手机号')
        return false;
      }
      var params = {
        phone: this.data.userInfo.phone,
      };
      this.requestUpdate(config.service.updatePhoneUrl,params);

    }else{

      if (e.detail.value.nameText.length == 0) {
        this.show('请输入名前');
        return;
      }
      if (e.detail.value.nameText.length < 2) {
        this.show('名前を変更ルール (2-10个字)');
        return;
      }

      var params = {
        name: e.detail.value.nameText,
      };
      this.requestUpdate(config.service.updateNameUrl, params);

    }
    
  },

  requestUpdate:function(url,params){
    console.log('参数',params);

    net.doRequest(url, params, 'POST', true, net.Token, res => {
      if (res.code == 0) {
        this.show('成功');
        wx.navigateBack();

      }
    });
  },

  getPhoneNumber: function (e) {
    var that = this;
    wx.login({
      success: res => {
        if (res.code) {
          this.getSesssionKey(res.code, e.detail.encryptedData, e.detail.iv, e);
        }
      },
      fail: function () {

      }
    });

  },

  getSesssionKey(code, encryData, ivData, e) {
    var that = this;
    var pa = {
      code: code
    }

    net.doRequest(config.service.getSessionKey, pa, 'POST', true, net.Token, res => {
      console.log('sessionKey', res);
      if (res.code == 0) {

        wx.checkSession({
          success: function () {
            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
              that.setData({
                modalstatus: true
              });
            } else {//同意授权

              var params = {
                encrypData: encryData,
                ivData: ivData,
                sessionKey: res.data,
              }
              console.log('张张', params);

              net.doRequest(config.service.getWXPhoneUrl2, params, 'POST', true, net.Token, res => {
                //console.log('电话', res);
                if(res.code == 0){
                  var data = JSON.parse(res.data);
                  console.log('落落',that,that.data);
                  that.data.userInfo.phone = data.phoneNumber
                  that.setData({
                    userInfo:that.data.userInfo
                  })

                }

              })
            }
          }

        });

      }
    });
  }

  // getPhoneNumber: function (e) {

  // }









})